package com.boilerCloud.controller.device;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.ModelInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.device.IModelService;
import com.boilerCloud.service.log.ILogService;
import com.boilerCloud.util.FileUtil;


@RestController
@RequestMapping("/model/")
public class ModelControler {
	private static final Logger log = LoggerFactory.getLogger(ModelControler.class);
	@Resource
	private IModelService modelService;

	@Resource
	private ILogService logService;

	@RequestMapping(value = "modelList")
	public ModelAndView accountList() {
		return new ModelAndView("/views/model/modelInfo.html");
	}
	
	

	/**
	 * 查询模组列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryModelList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryModelList(@ModelAttribute ModelInfo model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<ModelInfo> page = new HzlqswPage<ModelInfo>();
			page.setCurPage(yema);
			page.setPageSize(length);
			modelService.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询模组列表失败");
			log.error("查询模组列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改设备型号信息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editModel", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editModel(
			@ModelAttribute  ModelInfo model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			String image = model.getPicUrl();
			String root = request.getSession().getServletContext().getRealPath("/");
			image = FileUtil.upload(root, image);
			model.setPicUrl(image);

			modelService.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			logService.addLog(request,"修改设备型号","修改了设备型号"+model.getName(),model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改模组信息失败");
			log.error("修改模组信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增模组
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增设备型号")
	@RequestMapping(value = "addModel", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addModel(
			@ModelAttribute  ModelInfo model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			ModelInfo byName = modelService.qryModelInfoByName(model.getName());
			if(byName!=null){
				rst.setResultCode(ReqResult.resultCode_exist);
				rst.setReturnObject("["+model.getName()+"]已存在,请重新输入");
				return rst;
			}
			
			String image = model.getPicUrl();
			String root = request.getSession().getServletContext().getRealPath("/");
			image = FileUtil.upload(root, image);
			model.setPicUrl(image);
			modelService.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			logService.addLog(request,"新增设备型号","新增了设备型号"+model.getName(),model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增设备型号失败");
			log.error("新增设备型号异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除模组
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@LogAnnotation("删除设备型号")
	@RequestMapping(value = "delModel", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			modelService.del(id);
			logService.addLog(request,"删除设备型号","删除设备型号",id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除模组失败");
			log.error("删除模组异常",e);
			return rst;
		}
	}
	

	/**
	 * 获取模组列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getModelList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelList(
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			List<ModelInfo> list = modelService.getModelList(null);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(list);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取模组列表失败");
			log.error("获取模组列表异常",e);
			return rst;
		}
	}

	@RequestMapping(value = "getModelInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelInfo(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			ModelInfo model = modelService.getModelInfo(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取模组信息失败");
			log.error("获取模组信息异常",e);
			return rst;
		}
	}

}
